/*
 * Copyright @ 苏州瑞云信息技术有限公司 All rights reserved.
 */
package cn.com.recloud.bbs.service.impl;

import cn.com.recloud.bbs.constant.MetaOptionConstant;
import cn.com.recloud.bbs.dao.PostLikeLogDao;
import cn.com.recloud.bbs.entity.bbs_post_like_log;
import cn.com.recloud.bbs.entity.metaoption;
import cn.com.recloud.bbs.service.MetaoptionService;
import cn.com.recloud.bbs.service.PostLikeLogService;
import cn.com.recloud.bbs.util.UserUtil;
import cn.com.recloud.bbs.vo.MyLikeVO;
import cn.com.recloud.one.spring.service.impl.BaseServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author keblea xie
 * @date 2020-06-08
 */
@Service
public class PostLikeLogServiceImpl extends BaseServiceImpl<PostLikeLogDao, bbs_post_like_log> implements PostLikeLogService {
    @Resource
    PostLikeLogDao postLikeLogDao;

    @Autowired
    private MetaoptionService metaoptionService;
    @Autowired
    private UserUtil userUtil;

    /**
     * 我的点赞列表
     * @param name
     * @param type
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Override
    public List<MyLikeVO> findMyLikeList(String name,String type,int pageIndex,int pageSize) {
        String objectTypeName = null;
        Page<MyLikeVO> page = new Page<>(pageIndex, pageSize);
        String userId = userUtil.getUserId();
        //1活动，2文章
        if("1".equals(type)){
            objectTypeName = MetaOptionConstant.CAMPAIGN_ENTITY_NAME;
            return postLikeLogDao.findMyCampaignLikeList(objectTypeName,name,userId,page);
        }else{
            objectTypeName = MetaOptionConstant.ARTICAL_ENTITY_NAME;
            return postLikeLogDao.findMyArticleLikeList(objectTypeName,name,userId,page);
        }
    }

    /**
     * 点赞
     * @param bbsPostLikeLog
     * @return
     */
    @Override
    public boolean savePostLike(bbs_post_like_log bbsPostLikeLog) {
        bbsPostLikeLog.setLike_by_id(userUtil.getUserId());
        bbsPostLikeLog.setLike_by_name(userUtil.getUserName());
        LambdaQueryWrapper<bbs_post_like_log> lambda = new QueryWrapper<bbs_post_like_log>().lambda();
        lambda.eq(bbs_post_like_log::getObject_id,bbsPostLikeLog.getObject_id())
                .eq(bbs_post_like_log::getLike_by_id,bbsPostLikeLog.getLike_by_id());
        bbs_post_like_log bbsPostLikeLog1 = postLikeLogDao.selectOne(lambda);
        if(bbsPostLikeLog1 != null){
            //取消点赞
            return this.cancelPostLike(bbsPostLikeLog.getLike_by_id(),bbsPostLikeLog.getObject_id());
        }
        metaoption option = metaoptionService.findMetaOptionBySetNameAndValue(MetaOptionConstant.USER_SOURCE, "10");
        bbsPostLikeLog.setLike_by_type(option.getValue());
        bbsPostLikeLog.setLike_by_typename(option.getName());
        bbsPostLikeLog.setObject_type_name(MetaOptionConstant.ARTICAL_ENTITY_NAME);
        return super.save(bbsPostLikeLog);
    }

    /**
     * 取消点赞
     * @param userId
     * @param objectId
     * @return
     */
    @Override
    public boolean cancelPostLike(String userId, String objectId) {
        LambdaQueryWrapper<bbs_post_like_log> lambda = new QueryWrapper<bbs_post_like_log>().lambda();
        lambda.eq(bbs_post_like_log::getObject_id,objectId)
                .eq(bbs_post_like_log::getLike_by_id,userId);
        return super.remove(lambda);
    }
}
